<template>
	<div style="margin-left: 24%;margin-right: 24%;">
		<h1 style="text-align: center;font-size: 2rem;">编辑新闻</h1>
		<div style="margin: 2rem;">
			标题:<el-input v-model="news.title" placeholder="请输入标题"></el-input>
		</div>
		<div style="margin: 2rem;">
			作者:<el-input v-model="news.author" placeholder="请输入作者"></el-input>
		</div>
		<div style="margin: 2rem;">
			分类:
			<el-select v-model="news.category">
				<el-option label="国内" value="国内"></el-option>
				<el-option label="生活" value="生活"></el-option>
				<el-option label="军事" value="军事"></el-option>
				<el-option label="互联网" value="互联网"></el-option>
				<el-option label="金融" value="金融"></el-option>
				<el-option label="房地产" value="房地产"></el-option>
			</el-select>
		</div>
		<div style="margin: 2rem;">
			<el-input v-model="news.content" type="textarea" :rows="15" placeholder="请输入内容"></el-input>
		</div>
		<div style="margin: 2rem;">
			<el-button @click="cancel()">取消</el-button>
			<el-button @click="save()">保存</el-button>
		</div>
	</div>
</template>

<script>
	import axios from 'axios'
	axios.defaults.baseURL = '/api'
	import { ElMessage } from 'element-plus'
	export default{
		data() {
			return{
				news:{
					id:0,
					title:'',
					author:'',
					category:'',
					content:''
				}
			}
		},
		methods:{
			cancel(){
				this.$router.push('/newsList')
			},
			save(){
				axios.put('news',this.news)
				.then(response => {
					if(response.data){
						ElMessage.success('保存成功！')
						
					}else{
						ElMessage.success('保存失败！')
					}
					this.$router.push('/newsList')
				})
			}
		},
		mounted() {
			//新闻信息回显
			axios.get('news/findOne',{
				params:{
					id:this.$route.params.id
				}
			}).then(response => {
				this.news = response.data
			})
		}
	}
</script>

<style>
</style>